home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 17 controls / customcontroldemo / textboxex.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  8.6 KB  |  257 lines

  1. Imports System.Text.RegularExpressions
  2. Imports System.ComponentModel
  3. Imports System.Reflection
  4.  
  5. ' an improved TextBox control
  6.  
  7. <DefaultEvent("InvalidKey")> _
  8. Public Class TextBoxEx
  9.     Inherits System.Windows.Forms.TextBox
  10.  
  11.     Event InvalidKey(ByVal sender As Object, ByVal e As EventArgs)
  12.  
  13.     Sub New()
  14.         MyBase.New()
  15.     End Sub
  16.  
  17.     '-------------------------------------------------------------------
  18.     ' True if the field is required
  19.     '-------------------------------------------------------------------
  20.  
  21.     <Description("True if the field is required and must contain a value"), _
  22.              Category("Validation")> _
  23.     Dim m_IsRequired As Boolean
  24.  
  25.     Property IsRequired() As Boolean
  26.         Get
  27.             Return m_IsRequired
  28.         End Get
  29.         Set(ByVal Value As Boolean)
  30.             m_IsRequired = Value
  31.         End Set
  32.     End Property
  33.  
  34.     '-------------------------------------------------------------------
  35.     ' The text of the message 
  36.     '-------------------------------------------------------------------
  37.  
  38.     Dim m_ErrorMessage As String
  39.  
  40.     <Description("The error message displayed in case of error"), _
  41.          Category("Validation")> _
  42.    Property ErrorMessage() As String
  43.         Get
  44.             Return m_ErrorMessage
  45.         End Get
  46.         Set(ByVal Value As String)
  47.             m_ErrorMessage = Value
  48.         End Set
  49.     End Property
  50.  
  51.     '-------------------------------------------------------------------
  52.     ' The BeepOnError property (defaults to True)
  53.     '-------------------------------------------------------------------
  54.  
  55.     Dim m_BeepOnError As Boolean = True
  56.  
  57.     <Description("If True, a beep will be emitted if validation fails"), _
  58.      DefaultValue(True)> _
  59.     Property BeepOnError() As Boolean
  60.         Get
  61.             Return m_BeepOnError
  62.         End Get
  63.         Set(ByVal Value As Boolean)
  64.             m_BeepOnError = Value
  65.         End Set
  66.     End Property
  67.  
  68.     '-------------------------------------------------------------------
  69.     ' The ForeColor of the message 
  70.     '-------------------------------------------------------------------
  71.  
  72.     Dim m_ErrroForeColor As Color = SystemColors.ControlText
  73.  
  74.     <Description("The ForeColor of the DisplayControl in case of error"), _
  75.      Category("Validation")> _
  76.     Property ErrorForeColor() As Color
  77.         Get
  78.             Return m_ErrroForeColor
  79.         End Get
  80.         Set(ByVal Value As Color)
  81.             m_ErrroForeColor = Value
  82.         End Set
  83.     End Property
  84.  
  85.     Sub ResetErrorForeColor()
  86.         ErrorForeColor = SystemColors.ControlText
  87.     End Sub
  88.  
  89.     Function ShouldSerializeErrorForeColor() As Boolean
  90.         Debug.WriteLine("ShouldSerializeErrorForeColor for " & Me.Name)
  91.         Return Not Me.ErrorForeColor.Equals(SystemColors.ControlText)
  92.     End Function
  93.  
  94.     '-------------------------------------------------------------------
  95.     ' The ValidateRegex property
  96.     '-------------------------------------------------------------------
  97.  
  98.     <Description("The regular expression used to validate the control on exit"), _
  99.              Category("Validation")> _
  100.     Property ValidateRegex() As String
  101.         Get
  102.             Return m_ValidateRegex
  103.         End Get
  104.         Set(ByVal Value As String)
  105.             ' Before setting this mask, which check that its a valid regular expression
  106.             Try
  107.                 ' We attempt to use this mask as a regular expression
  108.                 If Value <> "" Then
  109.                     Dim dummy As Boolean = Regex.IsMatch("abcde", Value)
  110.                 End If
  111.                 ' if no error assignment is ok
  112.                 m_ValidateRegex = Value
  113.             Catch ex As Exception
  114.                 MessageBox.Show(ex.Message, "Invalid Property", MessageBoxButtons.OK, MessageBoxIcon.Error)
  115.             End Try
  116.         End Set
  117.     End Property
  118.  
  119.     Dim m_ValidateRegex As String
  120.  
  121.     '-------------------------------------------------------------------
  122.     ' The optional control that displays the error message
  123.     '-------------------------------------------------------------------
  124.  
  125.     Dim m_DisplayControl As Control
  126.  
  127.     <Description("The control that will display the error message"), _
  128.          Category("Validation")> _
  129.     Property DisplayControl() As Control
  130.         Get
  131.             Return m_DisplayControl
  132.         End Get
  133.         Set(ByVal Value As Control)
  134.             m_DisplayControl = Value
  135.         End Set
  136.     End Property
  137.  
  138.     '-------------------------------------------------------------------
  139.     ' The type of value the control must contain
  140.     '-------------------------------------------------------------------
  141.  
  142.     Enum ValidTypes
  143.         Any = 0
  144.         [Byte]
  145.         [Short]
  146.         [Integer]
  147.         [Long]
  148.         [Single]
  149.         [Double]
  150.         [Decimal]
  151.         [DateTime]
  152.     End Enum
  153.  
  154.     Dim m_ValidType As ValidTypes
  155.  
  156.     <Description("The type of value that is valid for this field"), _
  157.      Category("Validation"), DefaultValue(ValidTypes.Any)> _
  158.     Property ValidType() As ValidTypes
  159.         Get
  160.             Return m_ValidType
  161.         End Get
  162.         Set(ByVal Value As ValidTypes)
  163.             m_ValidType = Value
  164.         End Set
  165.     End Property
  166.  
  167.     '-------------------------------------------------------------------
  168.     ' The IsValid property
  169.     '-------------------------------------------------------------------
  170.  
  171.     ' This property has the same semantics as the Validate method.
  172.     ' (Just an example to show how you can hide a property.)
  173.  
  174.     <Browsable(False)> _
  175.     ReadOnly Property IsValid() As Boolean
  176.         Get
  177.             Return Validate()
  178.         End Get
  179.     End Property
  180.  
  181.     '-------------------------------------------------------------------
  182.     ' The Validate method
  183.     '-------------------------------------------------------------------
  184.  
  185.     <Description("Returns True if the current value passes the validation test")> _
  186.     Function Validate(Optional ByVal DisplayMessage As Boolean = True) As Boolean
  187.         ' Assume control passed the validation.
  188.         Validate = True
  189.  
  190.         ' Apply the IsRequired property.
  191.         If Me.IsRequired And Me.Text = "" Then
  192.             Validate = False
  193.         End If
  194.  
  195.         ' Apply the ValidType property.
  196.         If Validate = True And Me.Text <> "" Then
  197.             Validate = CheckValueType(Me.Text)
  198.         End If
  199.  
  200.         ' Apply the ValidateRegex property.
  201.         If Validate = True And Me.ValidateRegex <> "" Then
  202.             Validate = Regex.IsMatch(Me.Text, Me.ValidateRegex)
  203.         End If
  204.  
  205.         ' If the validation failed, the called asked to see a message box, 
  206.         ' the client defined a display control and an error message, then show the message on the control.
  207.         If DisplayMessage And Not (DisplayControl Is Nothing) And Me.ErrorMessage <> "" Then
  208.             If Validate Then
  209.                 ' Delete and previous error message.
  210.                 DisplayControl.Text = ""
  211.             Else
  212.                 ' Display error message, and enforce colors.
  213.                 DisplayControl.Text = Me.ErrorMessage
  214.                 DisplayControl.ForeColor = m_ErrroForeColor
  215.             End If
  216.         End If
  217.  
  218.         ' emit a beep in case of error, if so requested
  219.         If Not Validate And Me.BeepOnError Then Beep()
  220.     End Function
  221.  
  222.     ' Check that a value can be assigned to the value type 
  223.     Function CheckValueType(ByVal o As Object) As Boolean
  224.         Dim res As Object
  225.         Try
  226.             Select Case m_ValidType
  227.                 Case ValidTypes.Byte : res = CByte(o)
  228.                 Case ValidTypes.Short : res = CShort(o)
  229.                 Case ValidTypes.Integer : res = CInt(o)
  230.                 Case ValidTypes.Long : res = CLng(o)
  231.                 Case ValidTypes.Single : res = CSng(o)
  232.                 Case ValidTypes.Double : res = CDbl(o)
  233.                 Case ValidTypes.Integer : res = CDec(o)
  234.                 Case ValidTypes.DateTime : res = CDate(o)
  235.             End Select
  236.             ' if we get here, the assignment was ok
  237.             Return True
  238.         Catch
  239.             ' else there is an error
  240.             Return False
  241.         End Try
  242.     End Function
  243.  
  244.     ' this method is called when the control is about to raise the Validating event.
  245.  
  246.     Protected Overrides Sub OnValidating(ByVal e As System.ComponentModel.CancelEventArgs)
  247.         If Me.Validate() Then
  248.             ' If validation is ok, let the base class fire the Validating event.
  249.             MyBase.OnValidating(e)
  250.         Else
  251.             ' else, cancel the focus shift.
  252.             e.Cancel = True
  253.         End If
  254.     End Sub
  255.  
  256. End Class
  257.